import { useState, useMemo } from 'react'; import { useAppointmentStore } from '@/hooks/useAppointmentStore'; import { clinicInfo } from '@/mocks/clinicInfo'; import { toRepublicEraDate } from '@/utils/dateUtils'; const periodLabels: Record = { morning: '上午診', noon: '下午診', evening: '晚診', }; export default function PrintReportView() { const { doctors, getAppointmentsForDate, getDayActivePeriods, getAvailableSlots } = useAppointmentStore(); const [selectedDate, setSelectedDate] = useState(() => new Date().toISOString().split('T')[0]); const formattedDate = useMemo(() => { const d = new Date(selectedDate); const dayNames = ['日', '一', '二', '三', '四', '五', '六']; return `${d.getFullYear()} 年 ${d.getMonth() + 1} 月 ${d.getDate()} 日(${dayNames[d.getDay()]})`; }, [selectedDate]); const allAppointments = useMemo(() => getAppointmentsForDate(selectedDate), [selectedDate, getAppointmentsForDate]); const doctorReports = useMemo(() => { return doctors.map((doctor) => { const appts = allAppointments.filter((a) => a.doctorId === doctor.id); const periods = getDayActivePeriods(doctor.id, selectedDate); const slotGroups = getAvailableSlots(doctor.id, selectedDate); const totalSlots = slotGroups.reduce((sum, g) => sum + g.slots.length, 0); const bookedSlots = slotGroups.reduce((sum, g) => sum + g.slots.filter((s) => s.booked).length, 0); const isWorking = periods.length > 0; return { doctor, appts, periods, totalSlots, bookedSlots, isWorking }; }); }, [selectedDate, allAppointments, getDayActivePeriods, getAvailableSlots]); const workingDoctors = doctorReports.filter((r) => r.isWorking); const totalBooked = workingDoctors.reduce((sum, r) => sum + r.bookedSlots, 0); const totalSlotsAll = workingDoctors.reduce((sum, r) => sum + r.totalSlots, 0); const handlePrint = () => { window.print(); }; const PrintOnlyHeader = () => (

{clinicInfo.name}

{clinicInfo.address}

電話:{clinicInfo.phone}

每日預約報表

{formattedDate}

); return (
{/* Controls - hidden when printing */}

每日報表

選擇日期後點擊列印即可輸出報表

setSelectedDate(e.target.value)} className="px-3 py-2.5 rounded-lg border border-background-200/70 bg-background-50 text-foreground-900 text-sm focus:outline-none focus:ring-2 focus:ring-primary-400 focus:border-transparent cursor-pointer" />
{/* Report Content */}

{clinicInfo.name}

{formattedDate} 預約報表

總預約 {allAppointments.length} 總時段 {totalBooked}/{totalSlotsAll}
總預約 {allAppointments.length} 筆,總時段 {totalBooked}/{totalSlotsAll} 列印時間:{new Date().toLocaleString('zh-TW')}
{workingDoctors.length === 0 ? (

當日無看診醫師或尚無預約

) : (
{workingDoctors.map(({ doctor, appts, periods, totalSlots, bookedSlots }) => (
{doctor.name}
{doctor.name} {periods.map((p) => periodLabels[p.key] || p.key).join(' / ')}
已約 {bookedSlots}/{totalSlots} 位
{appts.length === 0 ? ( ) : ( appts.map((appt, idx) => ( )) )}
時段 姓名 電話 出生日期
尚無預約
{appt.time} {appt.patientName} {appt.patientPhone} {toRepublicEraDate(appt.patientBirthDate)}
))}
)}

{clinicInfo.name} | {clinicInfo.address} | 電話:{clinicInfo.phone}

); }